﻿// 用户登录

//查询cookie，如果cookie中存在UserCookie，说明此前已有账户登录过，且该账号选择了免密码登录
//故直接从cookie中读取该账户用户名和密码，自动登录
var userCookie = $.cookie("userCookie");
if(userCookie != null){
    $.ajax({
        type: "post",
        url: "/ysfwpt/userCookie",
        data: {
            userCookie: userCookie
        },
        success: function (data) {
            $("#username").val(data.username);
            $("#password").val(data.password);
        },
        error: function (data) {}
    })
}

var username;
var password;
$("#login").click(function (){
    username = $("#username").val();
    password = $("#password").val();
    if(username == ""){
        alert("请输入用户名！");
    }
    else if(password == ""){
        alert("请输入密码！");
    }
    else{
        console.log(username+":"+password);
        var automatic_logon = $('input:checkbox[name="automatic_logon"]:checked').val();
        if(automatic_logon == undefined){
            automatic_logon = "no"
        }
        $.ajax({
            type: "post",
            url: "/ysfwpt/login",
            data: {
                username: username,
                password: password,
                automatic_logon: automatic_logon
            },
            success: function(data) {
                // console.log("success");
                // console.log(data.result);
                switch(data.result){
                    case 1:
                        alert("该用户未注册!");
                        clean();
                        break;
                    case 2:
                        alert("用户名或密码错误!");
                        clean();
                        break;
                    case 3:
                        alert("账号已冻结!");
                        clean();
                        break;
                    case 4:
                        alert("账号已锁定，请联系管理员解除锁定！");
                        clean();
                        break;
                    case 5:
                        alert("账号未认证！");
                        clean();
                        break;
                    case 6:
                        alert("账号认证失败！");
                        clean();
                        break;
                    case 0:
                        console.log("登录成功!");
                        window.location.href = "ysfwpt_home?" + username;
                        break;
                }
            },
            error:function (data) {
                console.log("error");
                console.log(data);
            }
        });
    }
});

//清空输入框
function clean() {
    $("#password").val("");
}